package com.jinke.api.common.util;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.google.gson.Gson;
import com.jinke.api.modules.base.constant.RedisKeyConstants;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.commons.compress.utils.Lists;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.Scanner;

/**
 * 附加收费项
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SurchargeTool {

    private final RedisTemplate redisTemplate;

    /**
     * 说明：1.额外处理费-Packaging包装 无法判断不管 Additional Handling Surcharge - Packaging
     *      2.单位都是lbs,inches
     *      3.地址修改费Adress Correction 在预估环节 所以也不管
     *      4.特殊地区附加费Remote Area Surcharge包括：阿拉斯加Alaska+  夏威夷Hawaii+ US 48 收费各不同
     */

    /**
     * 是否符合服务
     * @param tableServiceCode
     * @param requestServiceCode
     * @return
     */
    public static boolean containsServiceCode(String tableServiceCode, String requestServiceCode) {
        if (StringUtils.isBlank(tableServiceCode)){
            //导入中没有这个限制直接都符合
            return true;
        }
        if (tableServiceCode.contains("/")){

            String[] split = tableServiceCode.split("/");
            for (String s : split) {
                if (requestServiceCode.contains(s)||requestServiceCode.contains(s.toUpperCase())){
                    return true;
                }
            }
            return false;
        }else {
            return requestServiceCode.contains(tableServiceCode);
        }
    }

    /**
     * 是否峰值时断
     * @return
     */
    public static boolean checkPeak() {
        LocalDate currentDate = LocalDate.now();
        int month = currentDate.getMonthValue();

        if (month >= 10 && month <= 12) {
           return true;
        } else {
            return false;
        }
    }

    /**
     * 是否符合zone
     * @param
     * @param
     * @return
     */
    public static boolean findByZone(String zones, String zone) {
        if (StringUtils.isBlank(zones)){
            //导入中没有这个限制直接都符合
            return true;
        }
        if (zones.contains(",")){

            String[] split = zones.split(",");
            for (String s : split) {

                if (s.equals(zone)){
                    return true;
                }
            }
            return false;
        }else {
            return zones.equals(zone);
        }
    }

    /**
     * 判断是非常重、超重还是只要额外收钱
     *
     * @param weightInLbs
     * @param length
     * @param width
     * @param height
     * @return
     */
    public static Integer checkSize(BigDecimal weightInLbs, BigDecimal length, BigDecimal width, BigDecimal height) {
        BigDecimal longestSide = getMax(length, width, height);
        BigDecimal secondLongestSide = getSecondMax(length, width, height);
        BigDecimal weichang = length.add(width.multiply(new BigDecimal(2)).add(height.multiply(new BigDecimal(2))));

        // 超重、超大件限制，不用分 zone
        if (weightInLbs.compareTo(new BigDecimal(150)) >= 0
                || longestSide.compareTo(new BigDecimal(108)) >= 0
                || weichang.compareTo(new BigDecimal(165)) >= 0) {
            return 3;
        }
        // 超大包裹费 - Oversize 要分住宅
        else if (longestSide.compareTo(new BigDecimal(96)) >= 0
                || weichang.compareTo(new BigDecimal(130)) >= 0) {
            return 2;
        }
        // 额外要收钱
        else if ((longestSide.compareTo(new BigDecimal(48)) > 0
                && longestSide.compareTo(new BigDecimal(96)) < 0)
                || secondLongestSide.compareTo(new BigDecimal(30)) >= 0
                || (weichang.compareTo(new BigDecimal(105)) > 0
                && weichang.compareTo(new BigDecimal(130)) < 0)) {
            return 1;
        } else {
            return 0;
        }
    }

    private static BigDecimal getMax(BigDecimal length, BigDecimal width, BigDecimal height) {
        return length.max(width).max(height);
    }

    private static BigDecimal getSecondMax(BigDecimal length, BigDecimal width, BigDecimal height) {
        BigDecimal max = getMax(length, width, height);
        BigDecimal min = length.min(width).min(height);
        BigDecimal middle = length.add(width).add(height).subtract(max).subtract(min);
        return middle;
    }

    /**
     * 额外处理费-Weight重量
     *Additional Handling Surcharge - Weight
     * @param weightInPounds
     * @return
     */
    public static boolean checkWeight(BigDecimal weightInPounds) {
        double v = weightInPounds.doubleValue();
        if ( v> 50 && v < 150) {
            return true;
        } else {
            return false;
        }
    }


    /**
     * 查距离是否超远 标远1 超远2 遥远Remote US 48 Zip 3 ak 4 hi 5  0是正常
     * 细分遥远345，因为有的模板3，4，5是分开收费项
     * @param zipCode
     * @return
     */
    public static Integer checkDistance(String zipCode) {
        //在US 48 Zip 标远1
        //在US 48 Zip DAS Extended 超远2
        //在Remote US 48 Zip + ak+hi 遥远
//        return  this.checkDistanceInRedis(zipCode);
        return 2;
    }


    /*************************偏远和超偏远2项**************************************/
    /**
     * 偏远地区
     * 要分住宅和商业地址收费
     * DAS：Delivery Area Surcharge
     * US 48 Zip DAS Extended表单
     * @param zipCode
     * @return true 存在  false 不存在
     */
    public boolean checkDistanceInRedis(String zipCode) {
        //List<String> remoteZipCodeList=new ArrayList<>();
        boolean exist = false;
        // US 48 Zip DAS Extended表单数据
        List<String> dasZipCodeList = redisTemplate.opsForList().range(RedisKeyConstants.ZIP_CODE_PREFIX + RedisKeyConstants.DAS_ZIP_CODE_PREFIX, 0, -1);
        if (Optional.ofNullable(dasZipCodeList).orElse(Lists.newArrayList())
                .stream().anyMatch(item -> item.equals(zipCode))) {
            exist = true;
        } else {
            // 没有匹配的 拿缓存里另外四个list 一一匹配
            // US 48 Zip表单数据
            List<String> us48ZipCodeList  = redisTemplate.opsForList().range(RedisKeyConstants.ZIP_CODE_PREFIX + RedisKeyConstants.US_48_ZIP_CODE_PREFIX, 0, -1);
            if (Optional.ofNullable(us48ZipCodeList).orElse(Lists.newArrayList())
                    .stream().anyMatch(item -> item.equals(zipCode))) {
                exist = true;
            } else {
                // HI-夏威夷 表单数据
                List<String> hiZipCodeList = redisTemplate.opsForList().range(RedisKeyConstants.ZIP_CODE_PREFIX + RedisKeyConstants.HI_ZIP_CODE_PREFIX, 0, -1);
                if (Optional.ofNullable(hiZipCodeList).orElse(Lists.newArrayList())
                        .stream().anyMatch(item -> item.equals(zipCode))) {
                    exist = true;
                } else {
                    // AK-阿拉斯加 表单数据
                    List<String> akZipCodeList = redisTemplate.opsForList().range(RedisKeyConstants.ZIP_CODE_PREFIX + RedisKeyConstants.AK_ZIP_CODE_PREFIX, 0, -1);
                    if (Optional.ofNullable(akZipCodeList).orElse(Lists.newArrayList())
                            .stream().anyMatch(item -> item.equals(zipCode))) {
                        exist = true;
                    } else {
                        // Remote US 48 Zip表单数据
                        List<String> easZipCodeList = redisTemplate.opsForList().range(RedisKeyConstants.ZIP_CODE_PREFIX + RedisKeyConstants.EAS_ZIP_CODE_PREFIX, 0, -1);
                        if (Optional.ofNullable(easZipCodeList).orElse(Lists.newArrayList())
                                .stream().anyMatch(item -> item.equals(zipCode))) {
                            exist = true;
                        }
                    }
                }
            }
        }
        return exist;
    }
    /**
     * 超偏远地区
     * 要分住宅和商业地址收费
     * Extended Area Surcharge
     * Remote US 48 Zip表单
     * @param zipCode
     * @return
     */


    /**
     * 夏威夷Hawaii
     * Remote HI Zip
     * @param zipCode
     * @return
     */

    /**
     * 阿拉斯加Alaska
     * Remote AK Zip
     * @param zipCode
     * @return
     */





    //住宅派送费Residential


}
